package com.freesky.quartz.task;

import com.alibaba.fastjson2.JSON;
import com.freesky.biz.activity.domain.TRechargeDetail;
import com.freesky.biz.activity.mapper.RechargeDetailMapper;
import com.freesky.biz.external.service.MobileCmccService;
import com.freesky.biz.external.service.MobileJuCaiService;
import com.freesky.biz.external.service.MobileJulivesService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.config.Constant;
import com.freesky.common.constant.Constants;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.utils.StringUtils;
import com.freesky.framework.web.service.SendAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 话费充值服务
 */
@Component("rechargeDetailTask")
public class RechargeDetailTask {

    private static final Logger logger = LoggerFactory.getLogger(PointExchangeTask.class);

    @Autowired
    private RechargeDetailMapper rechargeDetailMapper;

    @Autowired
    private MobileCmccService mobileCmccService;

    @Autowired
    private MobileJuCaiService mobileJuCaiService;

    @Autowired
    private MobileJulivesService mobileJulivesService;

    @Autowired
    private SendAlarmService sendAlarmService;

    public void taskProcess() {
        List<TRechargeDetail> rechargeDetailList = rechargeDetailMapper.selectUnRechargeDetailList();
        for(TRechargeDetail rechargeDetail : rechargeDetailList){
            int nSendCount = rechargeDetailMapper.getRechargeMobileLogCount(rechargeDetail.getId());
            if (nSendCount >= 3) {
                rechargeDetailMapper.updateProcessedById(rechargeDetail.getId(), "5");
                sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL, "【异常】JOB-积分兑话费-超过三次",
                        "次数【" + nSendCount + "】：" + JSON.toJSONString(rechargeDetail));
                continue;
            }

            //防误更改操作
            if(StringUtils.isNotEmpty(rechargeDetail.getVoucherNo())){
                rechargeDetailMapper.updateProcessedById(rechargeDetail.getId(), "6");
            } else {
                rechargeDetailMapper.updateProcessedById(rechargeDetail.getId(), "1");
                processRecharge(rechargeDetail);
            }
        }
    }


    //处理充值
    public void processRecharge(TRechargeDetail rechargeDetail){
        if(StringUtils.isEmpty(rechargeDetail.getChannel())){
            sendMobileCmccRecharge(rechargeDetail);
        } else if("移动".equalsIgnoreCase(rechargeDetail.getChannel())){
            sendMobileRechargeJuCai(rechargeDetail);
        } else if("聚采".equalsIgnoreCase(rechargeDetail.getChannel())){
            sendMobileRechargeJulive(rechargeDetail);
        } else {
            sendMobileRechargeJuCai(rechargeDetail);
        }
    }

    //移动充值
    public void sendMobileCmccRecharge(TRechargeDetail rechargeDetail){
        try{
            rechargeDetail.setLastReqId(SeqUtil.getSeqId());
            AjaxResult result = mobileCmccService.sendMobileRecharge(
                    rechargeDetail.getLastReqId(),
                    String.valueOf(rechargeDetail.getAmt()*10),
                    rechargeDetail.getMobile());

            rechargeDetail.setChannel("移动");
            if(result.isSuccess()) {
                rechargeDetail.setRespCode(Constants.SUCCESS);
                rechargeDetail.setRespErrmsg("充值成功");
                rechargeDetail.setProcessed("4");
                rechargeDetail.setRechargeId(rechargeDetail.getLastReqId());
                rechargeDetail.setVoucherNo(rechargeDetail.getLastReqId());
                rechargeDetail.setState("1");
            } else {
                rechargeDetail.setRespCode(Constants.FAIL);
                rechargeDetail.setRespErrmsg(result.getMsg());
                rechargeDetail.setProcessed("3");
            }
            rechargeDetailMapper.updateRechargeResultById(rechargeDetail);
            rechargeDetailMapper.insertIntegralMobileLog(rechargeDetail);
        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL,"【异常】JOB-话费充值-移动接口",ioe.getMessage());
        }
    }

    //智信
    public void sendMobileRechargeJulive(TRechargeDetail rechargeDetail){
        try{
            rechargeDetail.setLastReqId(SeqUtil.getSeqId());
            AjaxResult result = mobileJulivesService.sendMobileRecharge(
                    "RC_" + rechargeDetail.getLastReqId(),
                    String.valueOf(rechargeDetail.getAmt()),
                    rechargeDetail.getMobile());

            rechargeDetail.setChannel("智信");
            rechargeDetail.setRespCode(result.get("code").toString());
            rechargeDetail.setRespErrmsg(result.getMsg());
            if(result.isSuccess()) {
                String orderSn = result.get("data").toString();
                rechargeDetail.setRechargeId(orderSn);
                rechargeDetail.setProcessed("2");
            } else {
                rechargeDetail.setProcessed("3");
            }
            rechargeDetailMapper.updateRechargeResultById(rechargeDetail);
            rechargeDetailMapper.insertIntegralMobileLog(rechargeDetail);
        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL,"【异常】JOB-话费充值-智信接口",ioe.getMessage());
        }
    }


    public void sendMobileRechargeJuCai(TRechargeDetail rechargeDetail){
        try{
            rechargeDetail.setLastReqId(SeqUtil.getSeqId());
            AjaxResult result = mobileJuCaiService.sendMobileRecharge(
                    "RC_" + rechargeDetail.getLastReqId(),
                    String.valueOf(rechargeDetail.getAmt()),
                    rechargeDetail.getMobile());
            rechargeDetail.setChannel("聚采");
            rechargeDetail.setRespCode(result.get("code").toString());
            rechargeDetail.setRespErrmsg(result.getMsg());

            if(result.isSuccess()){
                rechargeDetail.setRechargeId("");
                rechargeDetail.setRechargeId(rechargeDetail.getLastReqId());
                rechargeDetail.setProcessed("2");
            } else {
                rechargeDetail.setProcessed("3");
            }
            rechargeDetailMapper.updateRechargeResultById(rechargeDetail);
            rechargeDetailMapper.insertIntegralMobileLog(rechargeDetail);
        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail(Constant.ALARM_MAIL,"【异常】JOB-话费充值-聚采接口",ioe.getMessage());
        }
    }
}
